/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */

package com.googlecode.chigscommons.web.plugin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.MetaDataKey;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

import brix.web.generic.BrixGenericPanel;
import brix.workspace.Workspace;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
public class ServicesPanel extends BrixGenericPanel<Workspace> {
	private static final String EDITOR_ID = "editor";

    private static MetaDataKey<String> EDITOR_NODE_TYPE = new MetaDataKey<String>() {
    };
    
    private Component editor;

	public ServicesPanel(String id, IModel<Workspace> workspaceModel) {
		super(id, workspaceModel);

		editor = new WebMarkupContainer(EDITOR_ID);
		add(editor);
		
		setupDefaultEditor();

		WebMarkupContainer createNodesContainer = new WebMarkupContainer("servicesContainer");
		add(createNodesContainer);

		createNodesContainer.add(new ListView<ServicePluginEntry>("services", createServicesModel) {
			@Override
			protected void populateItem(final ListItem<ServicePluginEntry> item) {
				Link<Void> link;
				item.add(link = new Link<Void>("link") {
					@Override
					public void onClick() {
						ServicePlugin plugin = item.getModelObject().getPlugin();
						Panel panel = plugin.newServicePanel(EDITOR_ID);
						panel.setMetaData(EDITOR_NODE_TYPE, plugin.getServiceType());
						setupEditor(panel);
					}

                    @Override
                    protected void onComponentTag(ComponentTag tag) {
                        super.onComponentTag(tag);
                        ServicePlugin plugin = item.getModelObject().getPlugin();
                        String editorNodeType = getEditor().getMetaData(EDITOR_NODE_TYPE);
                        if (plugin.getServiceType().equals(editorNodeType)) {
                            CharSequence klass = tag.getString("class");
                            if (klass == null) {
                                klass = "selected";
                            } else {
                                klass = klass + " selected";
                            }
                            tag.put("class", klass);
                        }
                    }					
				});
				item.add(new WebMarkupContainer("separator") {
					@Override
					public boolean isVisible() {
						return item.getIndex() != createServicesModel.getObject().size() - 1;
					}
				});
				ServicePlugin plugin = item.getModelObject().getPlugin();
				link.add(new Label("label", plugin.newServiceCaptionModel()));
			}

		}.setReuseItems(false));
	}

	private IModel<List<ServicePluginEntry>> createServicesModel = new LoadableDetachableModel<List<ServicePluginEntry>>() {
		@Override
		protected List<ServicePluginEntry> load() {
			List<ServicePluginEntry> result = new ArrayList<ServicePluginEntry>();
			for (ServicePlugin plugin : ServicesPlugin.get().getServicePlugins()) {
				if (plugin.newServiceCaptionModel() != null) {
					result.add(new ServicePluginEntry(plugin));
				}
			}
			return result;
		}
	};

    private Component getEditor() {
        return get(EDITOR_ID);
    };
    
    private void setupEditor(Component newEditor) {
		editor.replaceWith(newEditor);
		editor = newEditor;
	}

	private void setupDefaultEditor() {
		setupEditor(new EmptyPanel(EDITOR_ID));
	}

	private static class ServicePluginEntry implements Serializable {

		private final String serviceType;

		public ServicePluginEntry(ServicePlugin plugin) {
			this.serviceType = plugin.getServiceType();
		}

		public ServicePlugin getPlugin() {
			return ServicesPlugin.get().getServicePluginForType(serviceType);
		}
	};
}
